package com.zhuzg.common.interceptor;

import com.zhuzg.common.config.TenantContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
@Component
public class DsHandlerInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取租户
        String tenant = request.getHeader("tenantId");

        if (!StringUtils.hasText(tenant)){
            // 没有给出该参数 就是用默认的数据库
            tenant="default";
        }
        TenantContext.setTenantId(tenant);
        return true;

    }
}
